/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.awt;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;

public class CanvasState 
{
	private AffineTransform m_transform = new AffineTransform();
	private double globalAlpha = 1.0;
	private java.awt.Color m_fillColor = java.awt.Color.white;
	private java.awt.Color m_strokeColor = java.awt.Color.white;
	private double m_lineWidth = 1.0;
	private int    m_lineMiter = 1;
	private Stroke m_lineWidthShape = new BasicStroke((float) 1.0);
	
	public CanvasState()
	{
	}

	public CanvasState(CanvasState state)
	{
		m_transform = (AffineTransform)state.getTransform().clone();
		m_fillColor = state.m_fillColor;
		m_strokeColor = state.m_strokeColor;
	}

	public java.awt.Color getFillColor() {
		return m_fillColor;
	}

	public void setFillColor(java.awt.Color col) 
	{
		int alpha = (int)(255.0*getGlobalAlpha());
		java.awt.Color fillcol = new java.awt.Color(col.getRed(), col.getGreen(), col.getBlue(), alpha);
		this.m_fillColor = fillcol;
	}

	public java.awt.Color getStrokeColor() {
		return m_strokeColor;
	}

	public void setStrokeColor(java.awt.Color col) 
	{
		int alpha = (int)(255.0*getGlobalAlpha());
		java.awt.Color strokecol = new java.awt.Color(col.getRed(), col.getGreen(), col.getBlue(), alpha);
		this.m_strokeColor = strokecol;
	}

	
	public AffineTransform getTransform()
	{
		return m_transform;
	}

	public double getGlobalAlpha() {
		return globalAlpha;
	}

	public void setGlobalAlpha(double globalAlpha) {
		this.globalAlpha = globalAlpha;
		int alpha = (int)(255.0*globalAlpha);
		m_strokeColor = new java.awt.Color(m_strokeColor.getRed(), m_strokeColor.getGreen(), m_strokeColor.getBlue(), alpha);
		m_fillColor = new java.awt.Color(m_fillColor.getRed(), m_fillColor.getGreen(), m_fillColor.getBlue(), alpha);		
	}

	public void applyFillColor(Graphics2D g)
	{
		g.setColor(m_fillColor);
	}

	public void applyStrokeColor(Graphics2D g)
	{
		g.setColor(m_strokeColor);
		g.setStroke(m_lineWidthShape);
	}

	private void updateStrokeStyle()
	{
		this.m_lineWidthShape = new BasicStroke((float)m_lineWidth);
	}
	
	public void setLineWidth(double lineWidth) {
		this.m_lineWidth = lineWidth;
		this.updateStrokeStyle();
	}

	public double getLineWidth() {
		return m_lineWidth;
	}

	
}
